package com.lineage.data.cmd;

import com.lineage.config.ConfigRate;
import com.lineage.data.item_etcitem.add.newskillbook;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;

import java.util.Random;

public abstract class EnchantExecutor {
  public abstract void failureEnchant(L1PcInstance paramL1PcInstance, L1ItemInstance paramL1ItemInstance);
  
  public abstract void successEnchant(L1PcInstance paramL1PcInstance, L1ItemInstance paramL1ItemInstance, int paramInt);
  
  public int randomELevel(L1ItemInstance tgtitem, int scrollBless) {
    Random random;
    int i, level = 0;
    random = new Random();
    i = random.nextInt(100) + 1;
    int enlv = tgtitem.getEnchantLevel();
    int weaponRate = ConfigRate.ENCHANT_CHANCE_WEAPON;
    int armorRate =ConfigRate.ENCHANT_CHANCE_ARMOR;

    switch (scrollBless) {
      case 0:   //祝福
      case 128:
        if (tgtitem.getBless() >= 3)
          break;       
        //处理武器
        if (tgtitem.getItem().getType2() == 1) {
        	//处理当前加成-1
        	if (enlv <= -1) {
					if (i <= 20) {
						level = 3;
						break;
					} else if (i <= 50) {
						level = 2;
						break;
					} else {
						level = 1;
						break;
					}
				}
        	//处理安定0
         if (tgtitem.getItem().get_safeenchant() == 0) {
			if (enlv >= 0 && enlv <=5) {
				if (i <= weaponRate / 5) {
					level=3;
					break;
				}
				if (i<= weaponRate / 2) {
					level=2;
					break;
				}
				if (i<weaponRate) {
					level=1;
					break;
				}
				level= 99;
				break;
			}
			if (enlv >= 6 && enlv <=8) {
				if (i<=weaponRate) {
					level=1;
					break;
				}
				level = 99;
				break;
			}
			if (enlv >= 9) {
				if (i<=weaponRate * ( (100 - enlv) / 100 ) /2) {
					level=1;
					break;
				}
				if (i<=weaponRate * ( (100 - enlv) / 100 ) ) {
					level=0;
					break;
				}
				level = 99;
				break;
			}
		}
        if (tgtitem.getItem().get_safeenchant() == 6) {
			if (enlv >=0 && enlv <=5) {
				if (i<=(weaponRate + ( 7 - enlv) * 5 ) / 5) {
					level=3;
					break;
				}
				if (i<=(weaponRate + ( 7 - enlv) * 5 ) / 2) {
					level=2;
					break;
				}
				level = 1;
				break;
			}
			if (enlv >= 6 && enlv <=8) {
				if (i <= weaponRate) {
					level=1;
					break;
				}
				level = 99;
				break;
			}
			if (enlv >= 9) {
				if (i<=weaponRate * ( (100 - enlv) / 100 ) /2) {
					level=1;
					break;
				}
				if (i<=weaponRate * ( (100 - enlv) / 100 )) {
					level=0;
					break;
				}
				level = 99;
				break;
			}
		} 
        if (tgtitem.getItem().get_safeenchant() == 9) {
			if (enlv >=0 && enlv <=5) {
				if (i<=(weaponRate + ( 7 - enlv) * 5 ) / 5) {
					level=3;
					break;
				}
				if (i<=(weaponRate + ( 7 - enlv) * 5 ) / 2) {
					level=2;
					break;
				}
				level = 1;
				break;
			}
			if (enlv >= 6 && enlv <=8) {
					level=1;
					break;
			}
			if (enlv >= 9) {
				if (i<=weaponRate * ( (100 - enlv) / 100 ) /2) {
					level=1;
					break;
				}
				if (i<=weaponRate * ( (100 - enlv) / 100 ) ) {
					level=0;
					break;
				}
				level = 99;
				break;
			}
		  }     	  
        }
        	  

        if (tgtitem.getItem().getType2() != 2)
          break; 
        
        //防具
        if (enlv<=-1) {
			if (i <= 20) {
				level = 3;
				break;
			} else if (i <= 50) {
				level = 2;
				break;
			} else {
				level = 1;
				break;
			}
		}
        if (tgtitem.getItem().get_safeenchant() == 0) {
			if (enlv >= 0 && enlv <=5) {
				if (i <= armorRate / 4) {
					level=3;
					break;
				}
				if (i<= armorRate / 2) {
					level=2;
					break;
				}
				if (i<armorRate) {
					level=1;
					break;
				}
				level= 99;
				break;
			}
			if (enlv >= 6 && enlv <=8) {
				if (i<=armorRate) {
					level=1;
					break;
				}
				level = 99;
				break;
			}
			if (enlv >= 9) {
				if (i<=armorRate * ( (100 - enlv) / 100 ) /2) {
					level=1;
					break;
				}
				if (i<=armorRate * ( (100 - enlv) / 100 ) ) {
					level=0;
					break;
				}
				level = 99;
				break;
			}
		}
        
        if (tgtitem.getItem().get_safeenchant() == 4) {
			if (enlv >= 0 && enlv <=3) {
				if (i <= (armorRate + ( 7 - enlv) * 5 ) / 5) {
					level=3;
					break;
				}
				if (i<= (armorRate + ( 7 - enlv) * 5 ) / 2) {
					level=2;
					break;
				}
				level=1;
				break;
			}
			if (enlv ==4 || enlv==5) {
				if (i <= (armorRate + ( 7 - enlv) * 5 ) / 5) {
					level=3;
					break;
				}
				if (i<= (armorRate + ( 7 - enlv) * 5 ) / 2) {
					level=2;
					break;
				}
				if (i<armorRate) {
					level=1;
					break;
				}
				level= 99;
				break;
			}
			if (enlv >= 6 && enlv <=8) {
				if (i<=armorRate) {
					level=1;
					break;
				}
				level = 99;
				break;
			}
			if (enlv >= 9) {
				if (i<=armorRate * ( (100 - enlv) / 100 ) /2) {
					level=1;
					break;
				}
				if (i<=armorRate * ( (100 - enlv) / 100 ) ) {
					level=0;
					break;
				}
				level = 99;
				break;
			}
        }
	        if (tgtitem.getItem().get_safeenchant() == 6) {
				if (enlv >= 0 && enlv <=5) {
					if (i <= (armorRate + ( 7 - enlv) * 5 ) / 5) {
						level=3;
						break;
					}
					if (i<= (armorRate + ( 7 - enlv) * 5 ) / 2) {
						level=2;
						break;
					}
					level=1;
					break;
				}
				if (enlv >= 6 && enlv <=8) {
					if (i<=armorRate) {
						level=1;
						break;
					}
					level = 99;
					break;
				}
				if (enlv >= 9) {
					if (i<=armorRate * ( (100 - enlv) / 100 ) /2) {
						level=1;
						break;
					}
					if (i<=armorRate * ( (100 - enlv) / 100 ) ) {
						level=0;
						break;
					}
					level = 99;
					break;
				}
	        }
		        if (tgtitem.getItem().get_safeenchant() == 9) {
					if (enlv >=0 && enlv <=5) {
						if (i<=(armorRate + ( 7 - enlv) * 5 ) / 5) {
							level=3;
							break;
						}
						if (i<=(armorRate + ( 7 - enlv) * 5 ) / 2) {
							level=2;
							break;
						}
						level = 1;
						break;
					}
					if (enlv >= 6 && enlv <=8) {
							level=1;
							break;
					}
					if (enlv >= 9) {
						if (i<=armorRate * ( (100 - enlv) / 100 ) /2) {
							level=1;
							break;
						}
						if (i<=armorRate * ( (100 - enlv) / 100 ) ) {
							level=0;
							break;
						}
						level = 99;
						break;
					}
			 }

      case 1:  //一般
      case 129:
			if (tgtitem.getBless() >= 3)
				break;
			if (tgtitem.getItem().getType2() == 1) {
				// 处理当前加成-1
				if (enlv <= -1) {
					level = 1;
					break;
				}
				if (tgtitem.getItem().get_safeenchant() == 0) {
					if (enlv >= 0 && enlv <= 8) {
						if (i < weaponRate) {
							level = 1;
							break;
						}
						level = 99;
						break;
					}
					if (enlv >= 9) {
						if (i <= weaponRate * ((100 - enlv) / 100) / 2) {
							level = 1;
							break;
						}
						if (i <= weaponRate * ((100 - enlv) / 100)) {
							level = 0;
							break;
						}
						level = 99;
						break;
					}
				}
				if (tgtitem.getItem().get_safeenchant() == 6) {
					if (enlv >= 0 && enlv <= 5) {
						level = 1;
						break;
					}
					if (enlv >= 6 && enlv <= 8) {
						if (i <= weaponRate) {
							level = 1;
							break;
						}
						level = 99;
						break;
					}
					if (enlv >= 9) {
						if (i <= weaponRate / 2) {
							level = 1;
							break;
						}
						if (i <= weaponRate) {
							level = 0;
							break;
						}
						level = 99;
						break;
					}
				}
				if (tgtitem.getItem().get_safeenchant() == 9) {
					if (enlv >= 0 && enlv <= 8) {
						level = 1;
						break;
					}
					if (enlv >= 9) {
						if (i <= weaponRate / 2) {
							level = 1;
							break;
						}
						if (i <= weaponRate) {
							level = 0;
							break;
						}
						level = 99;
						break;
					}
				}
			}
				if (tgtitem.getItem().getType2() != 2)
					break;
				if (tgtitem.getItem().getType2() == 2) {
					if (enlv <= -1) {
						level = 1;
						break;
					}
					if (tgtitem.getItem().get_safeenchant() == 0) {
						if (enlv >= 0 && enlv <= 8) {
							if (i < armorRate) {
								level = 1;
								break;
							}
							level = 99;
							break;
						}
						if (enlv >= 9) {
							if (i <= armorRate * ((100 - enlv) / 100) / 2) {
								level = 1;
								break;
							}
							if (i <= armorRate * ((100 - enlv) / 100)) {
								level = 0;
								break;
							}
							level = 99;
							break;
						}
					}
					if (tgtitem.getItem().get_safeenchant() == 4) {
						if (enlv >= 0 && enlv <= 3) {
							level = 1;
							break;
						}
						if (enlv == 4 || enlv == 5 || enlv == 6 || enlv == 7 || enlv == 8) {
							if (i <= armorRate) {
								level = 1;
								break;
							}
							level = 99;
							break;
						}
						if (enlv >= 9) {
							if (i <= armorRate / 2) {
								level = 1;
								break;
							}
							if (i <= armorRate) {
								level = 0;
								break;
							}
							level = 99;
							break;
						}
					}
					if (tgtitem.getItem().get_safeenchant() == 6) {
						if (enlv >= 0 && enlv <= 5) {
							level = 1;
							break;
						}
						if (enlv == 6 || enlv == 7 || enlv == 8) {
							if (i <= armorRate) {
								level = 1;
								break;
							}
							level = 99;
							break;
						}
						if (enlv >= 9) {
							if (i <= armorRate / 2) {
								level = 1;
								break;
							}
							if (i <= armorRate) {
								level = 0;
								break;
							}
							level = 99;
							break;
						}
					}
					if (tgtitem.getItem().get_safeenchant() == 9) {
						if (enlv >= 0 && enlv <= 8) {
							level = 1;
							break;
						}
						if (enlv >= 9) {
							if (i <= armorRate * ((100 - enlv) / 100) / 2) {
								level = 1;
								break;
							}
							if (i <= armorRate * ((100 - enlv) / 100)) {
								level = 0;
								break;
							}
							level = 99;
							break;
						}
					}
				}
			
      case 2: //诅咒
      case 130:
        if (tgtitem.getBless() >= 3)
        	break;
        if (enlv >= -6) {
            level = -1; 
            break;
		}else {
			level = 0;
			break;
		}
      case 3:  //幻象
      case 131:
        if (tgtitem.getBless() == 3)
        	if (enlv < 9) {
				level = 1;
				break;
			}else {
				level = 0;
				break;
			}
    } 

    return level;
  }
}
